package com.cjh.crm.controller;


import com.cjh.crm.bean.Customer;
import com.cjh.crm.model.CRUDResult;
import com.cjh.crm.model.PageResult;
import com.cjh.crm.service.ICustomerService;
import org.apache.catalina.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/customer")
public class CusomterController {

    @Autowired
    ICustomerService iCustomerService;

    /*
        /*
        客户列表
         */
    @RequestMapping("/customerList")
    public String CusomterList() {
        return "customer/customerList";
    }

    @ResponseBody
    @RequestMapping("/listjson")
    public PageResult<Customer> listLison(Customer customer, int page, int limit) {
        PageResult<Customer> result = iCustomerService.findPageResult(customer, page, limit);
        return result;
    }

    /*
    客户修改映射
     */
    @RequestMapping("/customerEdit")
    public String toCustomerEditPage(Model model, int cusNo) {
        Customer customer = iCustomerService.findCustomerById(cusNo);
        model.addAttribute("customer", customer);
        return "customer/customerEdit";
    }

    /*
    客户修改
     */
    @RequestMapping("/update")
    @ResponseBody
    public CRUDResult costomerUpdate(Customer customer) {
        CRUDResult result = new CRUDResult();
        result.setSuccess(0);
        result.setMsg("修改成功");
        iCustomerService.costomerUpdate(customer);
//        System.out.println("打印："+customer);
        return result;
    }

    /*
    客户添加映射
    */
    @RequestMapping("/customerAdd")
    public String toCustomerAddPage(Customer customer) {

        return "customer/customerAdd";
    }

    /*
    客户添加
    */
    @PostMapping(value = "/save")
    @ResponseBody
    public CRUDResult customerSave(Customer customer) {
        CRUDResult crudResult = new CRUDResult();
//        System.out.println(customer);
        crudResult.setSuccess(0);
        crudResult.setMsg("保存成功");
        iCustomerService.save(customer);
        return crudResult;
    }


    /*
    客户详情
     */
    @RequestMapping("/customerInfo")
    public String toCustomerInfoPage(Model model, int cusNo) {
        Customer customer = iCustomerService.findCustomerById(cusNo);
        model.addAttribute("customer", customer);
        return "customer/customerInfo";
    }

    /*
    删除客户
     */
    @RequestMapping("/customerDel")
    @ResponseBody
    public CRUDResult toCustomerDelPage(int cusNo) {
        CRUDResult crudResult = new CRUDResult();
        crudResult.setMsg("删除成功！");
        iCustomerService.delCustomer(cusNo);
        return crudResult;
    }
}

